package com.zonski.jbots.engine;

/**
 * Directions recognised in the game (yep, there's only 9), direction
 * numbers are sequential starting with north at 1, north-east at 2, east at
 * 3 etc...<br/>
 * NONE is set to 0 as a convenient default
 */
public final class Directions
{
    public static final int NONE  = 0;
    public static final int NORTH = 1;
    public static final int NORTH_EAST = 2;
    public static final int EAST  = 3;
    public static final int SOUTH_EAST = 4;
    public static final int SOUTH = 5;
    public static final int SOUTH_WEST = 6;
    public static final int WEST  = 7;
    public static final int NORTH_WEST = 8;

    /**
     * The value of the minimum direction value
     */
    public static final int MIN_DIRECTION = NORTH;

    /**
     * the value of the maximum direction value
     */
    public static final int MAX_DIRECTION = NORTH_WEST;

    private static final int DIV = MAX_DIRECTION - MIN_DIRECTION + 1;

    /** 
     * Indicates whether the direction is one of NORTH, SOUTH, EAST, or WEST
     */
    public static boolean isCardinal(int direction)
    {
        // all the cardinal directions are odd
        return (direction % 2) > 0;
    }

    public static final int getOppositeDirection(int direction)
    {
        if(direction != NONE)
        {
            return ((direction + MAX_DIRECTION/2)%MAX_DIRECTION)+(MIN_DIRECTION-1);
        }else{
            return NONE;
        }
    }

    public static final int addDirection(int direction, int delta)
    {
        int result;
        if(direction == NONE && delta != 0)
        {
            direction = NORTH;
        }
        if(direction != NONE)
        {
            int added = direction + delta - MIN_DIRECTION;
            while(added < 0)
            {
                added += DIV;
            }
            result = (added%DIV)+MIN_DIRECTION;
        }else{
            result = NONE;
        }
        return result;
    }

    private Directions()
    {
    }
}
